package com.sunthy.mall.controller.client;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.sunthy.mall.model.admin.Result;
import com.sunthy.mall.model.admin.bo.client.order.AddOrderBo;
import com.sunthy.mall.model.admin.bo.client.order.AskGoodsMsgBo;
import com.sunthy.mall.model.admin.bo.client.order.CarListBo;
import com.sunthy.mall.model.admin.bo.client.order.SendCommentBo;
import com.sunthy.mall.model.admin.vo.client.order.OrderCarBo;
import com.sunthy.mall.service.admin.order.OrderService;
import com.sunthy.mall.service.admin.order.OrderServiceImpl;
import com.sunthy.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/api/mall/order/*")
public class OrderServlet extends HttpServlet {
    private Gson gson = new Gson();
    private OrderService orderService = new OrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/addOrder".equals(pathInfo)) {
            addOrder(request, response);
        } else if ("/settleAccounts".equals(pathInfo)) {
            settleAccounts(request, response);
        } else if ("/sendComment".equals(pathInfo)) {
            sendComment(request, response);
        }
    }



    /**
     * 发送评价的逻辑:
     * 1.接收请求参数
     * 2.存储数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void sendComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        SendCommentBo sendCommentBo = null;
        // 校验参数
        try {
            sendCommentBo = gson.fromJson(requestBody, SendCommentBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 存储数据
        int code = orderService.sendComment(sendCommentBo);
        // 返回结果
        if (code==0){
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("评价异常")));

    }

    /**
     * 结算购物车的逻辑:
     * 1.接收请求参数
     * 2.更改数据
     * 3.返回参数
     *
     * @param request
     * @param response
     */
    private void settleAccounts(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        CarListBo carListBo = null;
        // 校验参数
        try {
            carListBo = gson.fromJson(requestBody, CarListBo.class);
        } catch (JsonSyntaxException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        if (carListBo == null) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 更改数据
        int code = orderService.settleAccountToCar(carListBo);
        // 返回结果
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("下单异常")));
    }

    /**
     * 添加订单的逻辑:
     * 1.接受请求参数
     * 2.添加数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void addOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String requestBody = HttpUtils.getRequestBody(request);
        // 校验参数
        AddOrderBo addOrderBo = null;
        try {
            addOrderBo = gson.fromJson(requestBody, AddOrderBo.class);
        } catch (JsonSyntaxException j) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 数据库添加
        int code = orderService.addOrder(addOrderBo);
        // 返回结果
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("添加异常异常")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        if ("/getOrderByState".equals(pathInfo)) {
            getOrderByState(request, response);
        } else if ("/deleteOrder".equals(pathInfo)) {
            deleteOrder(request, response);
        } else if ("/pay".equals(pathInfo)) {
            pay(request, response);
        } else if ("/confirmReceive".equals(pathInfo)) {
            confirmReceive(request, response);
        }
    }

    /**
     * 确认收获的逻辑:
     * 1.接收请求参数
     * 2.修改数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */

    private void confirmReceive(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求参数
        String idStr = request.getParameter("id");
        Integer id = null;
        // 校验参数
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 修改数据
        int code = orderService.payOrderById(id);
        // 返回结果
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("确认异常")));
    }

    /**
     * 单订单支付的逻辑:
     * 1.接收请求参数
     * 2.修改数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void pay(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取参数
        String idStr = request.getParameter("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 支付订单
        int code = orderService.payOrderById(id);
        // 返回结果
        if (code == 0) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("支付失败")));

    }

    /**
     * 删除订单的逻辑：
     * 1.接收请求参数
     * 2.删除订单
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取请求参数
        String idStr = request.getParameter("id");
        // 校验参数
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 删除订单
        orderService.orderDeleteById(id);
        // 返回结果
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 进入个人中心的逻辑:
     * 1.接收请求参数
     * 2.查询数据
     * 3.返回结果
     *
     * @param request
     * @param response
     */
    private void getOrderByState(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取参数
        String stateStr = request.getParameter("state");
        String token = request.getParameter("token");
        // 校验参数
        Integer state = null;
        try {
            state = Integer.parseInt(stateStr);
        } catch (NumberFormatException e) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        if (token == null || "".equals(token)) {
            response.getWriter().println(gson.toJson(Result.error("参数错误")));
            return;
        }
        // 查询数据
        List<OrderCarBo> orderCarBoList = orderService.getOrderByState(token, state);
        // 返回结果
        if (orderCarBoList != null) {
            response.getWriter().println(gson.toJson(Result.ok(orderCarBoList)));
            return;
        }
        response.getWriter().println(gson.toJson(Result.ok("购物为空,去买点东西吧")));
    }
}
